﻿using GlennQuigmire.API.Application.GDtos;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class TransmitReceiveSearchHander : IRequestHandler<TransmitReceiveSearchCommand, Result>
    {

        private readonly IBaseRepositorys<Product> productRepository;
        private readonly IBaseRepositorys<InboundOrderProduct> inboundOrderProductRepository;
        private readonly IBaseRepositorys<OutboundOrderProduct> outboundOrderProductRepository;
        private readonly IBaseRepositorys<InboundOrderType> inboundOrderTypeRepository;
        private readonly IBaseRepositorys<OutboundOrderType> outboundOrderTypeRepository;
        private readonly IBaseRepositorys<InboundOrder> inboundOrderRepository;
        private readonly IBaseRepositorys<OutboundOrder> outboundOrderRepository;

        public TransmitReceiveSearchHander(IBaseRepositorys<Product> productRepository, IBaseRepositorys<InboundOrderProduct> inboundOrderProductRepository, IBaseRepositorys<OutboundOrderProduct> outboundOrderProductRepository, IBaseRepositorys<InboundOrderType> inboundOrderTypeRepository, IBaseRepositorys<OutboundOrderType> outboundOrderTypeRepository, IBaseRepositorys<InboundOrder> inboundOrderRepository, IBaseRepositorys<OutboundOrder> outboundOrderRepository)
        {
            this.productRepository = productRepository;
            this.inboundOrderProductRepository = inboundOrderProductRepository;
            this.outboundOrderProductRepository = outboundOrderProductRepository;
            this.inboundOrderTypeRepository = inboundOrderTypeRepository;
            this.outboundOrderTypeRepository = outboundOrderTypeRepository;
            this.inboundOrderRepository = inboundOrderRepository;
            this.outboundOrderRepository = outboundOrderRepository;
        }


        /// <summary>
        /// 库存收发明细查询
        /// </summary>
        /// <param name="request">查询库存收发明细请求参数</param>
        /// <param name="cancellationToken">允许取消异步任务</param>
        /// <returns>返回库存收发明细查询结果</returns>
        public Task<Result> Handle(TransmitReceiveSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            TransmitReceiveDto transmitReceiveDto = new TransmitReceiveDto();
            var list1 = (from a in productRepository.GetAll()
                         join b in inboundOrderProductRepository.GetAll() on a.ProductId equals b.ProductId
                         join c in inboundOrderRepository.GetAll() on b.InboundOrderId equals c.InboundOrderId
                         join d in inboundOrderTypeRepository.GetAll() on c.InboundOrderTypeId equals d.InboundOrderTypeId
                         select new TransmitReceiveDto
                         {
                             ProductId = a.ProductId,
                             ProductNumber = a.ProductNumber,
                             ProductName = a.ProductName,
                             ProductSpecification = a.ProductSpecification,
                             StockUnit = a.StockUnit,
                             OrderNum = b.InboundOrderNum,
                             ChangeNum = b.InboundOrderNum,
                             ChangeTime = b.CreateDate,
                             OrderType = d.InboundOrderTypeName,
                             OrderNumber = c.InboundOrderNumber,
                             BoundOrderType = d.InboundOrderTypeName,
                             Remark = c.Remark,
                             CreateName = c.CreateName,
                             CreateTime = Convert.ToDateTime(c.CreateDate.ToString("yyyy-MM-dd HH:mm:ss")),
                             MaxInventory = a.MaxInventory,
                             MinInventory = a.MinInventory,
                             SaveInventory = a.SaveInventory,
                             InventoryStore = a.InventoryStore,
                         }).ToList();


            var list2 = (from a in productRepository.GetAll()
                         join b in outboundOrderProductRepository.GetAll() on a.ProductId equals b.ProductId
                         join c in outboundOrderRepository.GetAll() on b.OutboundOrderId equals c.OutboundOrderId
                         join d in outboundOrderTypeRepository.GetAll() on c.OutboundOrderTypeId equals d.OutboundOrderTypeId
                         select new TransmitReceiveDto
                         {
                             ProductId = a.ProductId,
                             ProductNumber = a.ProductNumber,
                             ProductName = a.ProductName,
                             ProductSpecification = a.ProductSpecification,
                             StockUnit = a.StockUnit,
                             OrderNum = b.OutNum,
                             ChangeNum = (0 - b.OutNum),
                             ChangeTime = c.CreateDate,
                             OrderType = d.OutboundOrderTypeName,
                             OrderNumber = c.OutboundOrderNumber,
                             BoundOrderType = d.OutboundOrderTypeName,
                             Remark = c.Remark,
                             CreateName = c.CreateName,
                             CreateTime = Convert.ToDateTime( c.CreateDate.ToString("yyyy-MM-dd HH:mm:ss")),
                             MaxInventory = a.MaxInventory,
                             MinInventory = a.MinInventory,
                             SaveInventory = a.SaveInventory,
                             InventoryStore = a.InventoryStore,
                         }).ToList();

            //合并两个集合
            list1.AddRange(list2);
            if (!string.IsNullOrEmpty(request.Numbers))
            {
                list1 = list1.Where(c => c.OrderNumber.Contains(request.Numbers)).ToList();
            }
            result.Data = list1.ToList();



            return Task.FromResult(result);

        }
    }
}
